<?php

namespace app\admin\controller;
use app\admin\controller\Base;

/**
 * 程序控制器
 */
class Software extends Base
{

    //程序列表
    public function softwarelist()
    {

        
        # code...
        $software=model('Software')->order(['id'])->paginate(10);
		$viewData=[
			'software'=>$software
		];
		$this->assign($viewData);
		return view();
    }


    public function softwareedit()
    {
        if (request()->isAjax()) {
            
			$data=[
                'id'=>input('post.id'),
				'softwarename'=>input('post.softwarename'),
                'version'=>input('post.version'),
                'notice'=>input('post.notice'),
                'heart_time'=>input('post.heart_time'),
                'software_up'=>input('post.software_up'),
                'software_state'=>input('post.software_state'),
                'machine_type'=>input('post.machine_type'),
                'machine_down'=>input('post.machine_down'),
                'machine_recoup'=>input('post.machine_recoup',0)

			];
			$result=model('Software')->edit($data);
			if ($result==1) {
				$this->success('栏目修改成功!','admin/software/softwarelist');
			}else{
				$this->error($result);
			}
		}
		$softwareInfo=model('Software')->find(input('id'));
		
		$viewData=[
			'softwareInfo'=>$softwareInfo

        ];
        
		$this->assign($viewData);
		return view();
    }





    //程序添加
    public function softwareadd()
    {
        # code...
        if (request()->isAjax()) {

            /**
             * name: ada ad
             *   version: 四川省菜市场
            *    notice: 四川省菜市场
             *   heart_time: 四川省菜市场
             *   software_up: sfsdfcfdcbdfsdvxzv
            *    software_state: 0
             *   machine_type: 1
             *   machine_down: 0
            *machine_recoup: 0
             */
           
            do
            {
                $software_key = bin2hex(random_bytes(4));
            }
            while (db('software')->where('software_key', $software_key)->find());

            $data=[
				'softwarename'=>input('post.softwarename'),
                'version'=>input('post.version'),
                'notice'=>input('post.notice'),
                'heart_time'=>input('post.heart_time'),
                'software_up'=>input('post.software_up'),
                'software_state'=>input('post.software_state'),
                'machine_type'=>input('post.machine_type'),
                'machine_down'=>input('post.machine_down'),
                'machine_recoup'=>input('post.machine_recoup',0),
                'software_key'=>$software_key

			];
			$result=model('Software')->add($data);
			if ($result==1) {
				$this->success('程序添加成功!','admin/software/softwarelist');
			}else{
				$this->error($result);
			}


        }
        return view();
    }
    public function softwaredel()
	{
		$softwareInfo=model('Software')->find(input('post.id'));
		$result=$softwareInfo->delete();
		if($result){
			$this->success('栏目删除成功','admin/software/softwarelist');
		}
		else{
			$this->error($result);
		}
	
	}

}